package cn.itcast.user.web;

import cn.itcast.user.config.PatternProperties;
import cn.itcast.user.pojo.User;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
//@RefreshScope
public class UserController {

    @Autowired
    private UserService userService;

    @Value("${server.port}")
    int port;

    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id, @RequestHeader(value = "name", required = false) String name) {
        System.out.println("请求头的值" + name);
        User user = userService.queryById(id);
        user.setAddress("" + port);
        return user;
    }

//    @Value("${pattern.dateformat}")
//    private String dateformat;

    @Autowired
    PatternProperties properties;

    @GetMapping("now")
    public String now() {
        // 换取当前时间  按照配置中的格式  进行时间格式化
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat()));
    }

    @GetMapping("prop")
    public PatternProperties prop() {
        return properties;
    }

    @PostMapping("save")
    public String save(@RequestBody User user) {
        System.out.println(user);
        return "保存成功了...";
    }
}